﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace YArchitech.Revit.Electric.Common
{
	public class Press
	{
		[DllImport("USER32.DLL")]
		public static extern bool PostMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);

		[DllImport("user32.dll")]
		private static extern uint MapVirtualKey(uint uCode, uint uMapType);

		private static void OneKey(IntPtr handle, char letter)
		{
			uint num = Press.MapVirtualKey((uint)letter, 0u);
			uint lParam = 1u | num << 16;
			uint lParam2 = 3221225473u | num << 16;
			Press.PostMessage(handle, 256u, (uint)letter, lParam);
			Press.PostMessage(handle, 257u, (uint)letter, lParam2);
		}

		public static void Keys(string command)
		{
			IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
			foreach (char letter in command)
			{
				Press.OneKey(mainWindowHandle, letter);
			}
		}

		private enum WH_KEYBOARD_LPARAM : uint
		{
			KEYDOWN = 1u,
			KEYUP = 3221225473u
		}

		private enum KEYBOARD_MSG : uint
		{
			WM_KEYDOWN = 256u,
			WM_KEYUP
		}

		private enum MVK_MAP_TYPE : uint
		{
			VKEY_TO_SCANCODE,
			SCANCODE_TO_VKEY,
			VKEY_TO_CHAR,
			SCANCODE_TO_LR_VKEY
		}
	}
}
